(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.profPastdueEditController', profPastdueEditController);
	profPastdueEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'arithService'];
	function profPastdueEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, arithService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("profPastdueEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfPastdueOptions : {
				url : 'do/cda/dataList/profPastdueOptions',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.filterCcys = ctrl.dialogsModule.params.filterCcys || [];
		ctrl.isEdit = ctrl.dialogsModule.params.isEdit;
		ctrl.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty || {};
		ctrl.formName = "profPastdueForm";
		
		ctrl.init = function() {
			ctrl.initSubTableDefine();
			if (ctrl.dialogsModule.params.isAdd == 'Y') {
				authService.initProfPastdue().then(function(response) {
					ctrl.initGeneral().then(function() {
						ctrl.profPastdue = angular.merge(response.data, ctrl.dialogsModule.params.profPastdue || {});
					});
				}, angular.noop);
			} else {
				ctrl.initGeneral().then(function() {
					ctrl.profPastdue = ctrl.dialogsModule.params.profPastdue || {};
					// for base rate customer
					initDataByProfPastdue();
				});
			}
		};
		
		ctrl.initGeneral = function() {
			var defer = $q.defer();
			// init pastdueSchd
			initPastdueSchd();
			// for base rate customer
			initCustTier();
			// for base rate mix
			initMixTier();
			
			var params = {};
			params.prodType = ctrl.prodSubtypeExtProperty.prodType;
			getProfPastdueOptions(params).then(function(response) {
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.ccyOptions = response.ccyOptions;
				ctrl.allIntRateIndexOptions = response.allIntRateIndexOptions;
				ctrl.allocCodeOptions = response.allocCodeOptions;
				ctrl.bufferDaysTypeOptions = response.bufferDaysTypeOptions;
				ctrl.intCalcBasisOptions = filterByProdSubtype("intCalcBasisOptions", response.intCalcBasisOptions);
				ctrl.pastdueIntCatOptions = response.pastdueIntCatOptions;
				ctrl.intRateTypeOptions = filterByProdSubtype("intRateTypeOptions", response.intRateTypeOptions);
				ctrl.allIntBaseTypeOptions = filterByProdSubtype("intBaseTypeOptions", response.intBaseTypeOptions);
				ctrl.intBookTypeOptions = response.intBookTypeOptions;
				ctrl.copyIntBookTypeOptions = angular.copy(response.intBookTypeOptions);
				ctrl.allIntBaseCustomAlgs = response.intBaseCustomAlgOptions;
				ctrl.intOnGraceperdOptions = response.intOnGraceperdOptions;
				defer.resolve();
			}, angular.noop);
			return defer.promise;
		};
		function getProfPastdueOptions(params) {
			var defer = $q.defer();
			ajaxService.getProfPastdueOptions(params).then(function(response) {
				var profPastdueOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						profPastdueOptions[key] = tempValue;
					});
				}
				defer.resolve(profPastdueOptions);
			});
			return defer.promise;
		}
		
		function filterByProdSubtype(type, options) {
			if (!options || options.length == 0) return options;
			var filter;
			if (type == 'intCalcBasisOptions') {
				filter = ctrl.prodSubtypeExtProperty.upIntCalcBases;
				if (!filter) {
					return options;
				}
			} else if (type == 'intRateTypeOptions') {
				filter = ctrl.prodSubtypeExtProperty.upIntRateTypes;
				if (!filter) {
					return options;
				}
			} else if (type == 'intBaseTypeOptions') {
				filter = ctrl.prodSubtypeExtProperty.upIntBaseTypes;
				if (!filter) {
					return options;
				}
			}
			
			var filterOptions = [];
			for (var i = 0; i < options.length; i++) {
				var loadVal = options[i].value;
				if (filter.indexOf(loadVal) >= 0) {
					filterOptions[filterOptions.length] = options[i];
				}
			}
			return filterOptions;
		}
		
		function initDataByProfPastdue() {
			if (ctrl.profPastdue.pastdueSchds) {
				var prePastdueTenor;
				for (var i = 0; i < ctrl.profPastdue.pastdueSchds.length; i++) {
					var pastdueSchd = ctrl.profPastdue.pastdueSchds[i];
					if (i == 0) {
						pastdueSchd.pastdueTenorFrom = 1;
						prePastdueTenor = pastdueSchd.pastdueTenor;
						continue;
					} else {
						pastdueSchd.pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
						prePastdueTenor = pastdueSchd.pastdueTenor;
					}
				}
			}
			
			if (ctrl.profPastdue.tierBaseRateCusts) {
				var preCustTierTenor;
				for (var i = 0; i < ctrl.profPastdue.tierBaseRateCusts.length; i++) {
					var tierBaseRateCust = ctrl.profPastdue.tierBaseRateCusts[i];
					if (i == 0) {
						tierBaseRateCust.tenerFrom = 0;
						preCustTierTenor = tierBaseRateCust.tener;
						continue;
					} else {
//						tierBaseRateCust.tenerFrom = arithService.add(preCustTierTenor, 1);
						tierBaseRateCust.tenerFrom = preCustTierTenor;
						preCustTierTenor = tierBaseRateCust.tener;
					}
				}
			}
		}
		
		ctrl.ccyChanged = function(value, form, isFirstLoad) {
			if (ctrl.profPastdue && ctrl.profPastdue.ccy) {
				if (ctrl.isEdit != 'Y') {// add page need check
					angular.forEach(ctrl.filterCcys, function(filterCcy, index) {
						if (ctrl.profPastdue.ccy && ctrl.profPastdue.ccy == filterCcy) {
							// changed eventCode has used
							ctrl.profPastdue.ccy = '';
							modalService.openErrorDialogs({
								messages : [ 'js.info.profpastdue.duplicate-ccy' ]
							});
							return;
						}
					});
				}
				// filter intRateIndex by ccy
				var intRateIndexOptions = [];
				for (var i = 0; i < ctrl.allIntRateIndexOptions.length; i++) {
					var _intIndex = ctrl.allIntRateIndexOptions[i];
					if (_intIndex.extProperty.ccy == ctrl.profPastdue.ccy) {
						intRateIndexOptions.push(ctrl.allIntRateIndexOptions[i]);
					}
				}
				ctrl.intRateIndexOptions = intRateIndexOptions;
				ctrl.copyIntRateIndexOptions = angular.copy(ctrl.intRateIndexOptions);
				
				resetIntRateIndexByIntBaseType(ctrl);
			}
		};
		
		ctrl.pastdueIntCatOptionsChanged = function(value) {
			if(ctrl.profPastdue.pastdueIntCat == 'P' || ctrl.profPastdue.pastdueIntCat == 'I') {
				var resultOptions = [];
				angular.forEach(ctrl.copyIntBookTypeOptions, function(obj) {
					if (!(obj.value == 'P' || obj.value == 'S')) {
						resultOptions.push(obj);
					}
				});
				ctrl.intBookTypeOptions = resultOptions;
			} else {
				ctrl.intBookTypeOptions = ctrl.copyIntBookTypeOptions;
			}
		};
		
		ctrl.intRateTypeChanged = function(value) {
			if (ctrl.profPastdue.intRateType) {
				// filter intRateIndex by ccy
				var intBaseTypeOptions = [];
				for (var i = 0; i < ctrl.allIntBaseTypeOptions.length; i++) {
					var _intBaseType = ctrl.allIntBaseTypeOptions[i];
					if (ctrl.profPastdue.intRateType == 'V') {
						// if intRateType 'Variable', intBaseType 'Custom' and 'Interpolation' cannot be selected 
						if (!(_intBaseType.value == 'C' || _intBaseType.value == 'I')) {
							intBaseTypeOptions.push(ctrl.allIntBaseTypeOptions[i]);
						}
					} else {
						intBaseTypeOptions.push(ctrl.allIntBaseTypeOptions[i]);
					}
				}
				ctrl.intBaseTypeOptions = intBaseTypeOptions;
			}
		};
		
		ctrl.intBaseTypeChanged = function(value, form, isInitialized) {
			if (!isInitialized && (!ctrl.profPastdue.intBaseType || ctrl.profPastdue.intBaseType == 'E')) {
					delete ctrl.profPastdue.intRateIndex;
			}
			if (ctrl.profPastdue.intBaseType) {
				resetIntBaseCustomAlgsByIntBaseType(ctrl);
				resetIntRateIndexByIntBaseType(ctrl);
				// need clear
				if (ctrl.profPastdue.intBaseType != 'C') {
					ctrl.profPastdue.tierBaseRateCusts = [];
				}
				// need clear
				if (ctrl.profPastdue.intBaseType != 'M') {
					ctrl.profPastdue.tierBaseRateMixs = [];
				}
			}
		};
		
		function resetIntBaseCustomAlgsByIntBaseType(ctrl) {
			var intBaseCustomAlgData = [];
			for (var i = 0; i < ctrl.allIntBaseCustomAlgs.length; i++) {
				var _intBaseCustomAlg = ctrl.allIntBaseCustomAlgs[i];
				var _filterTypes = _intBaseCustomAlg.extProperty.INT_BASE_TYPES;
				if (_filterTypes && _filterTypes.indexOf(ctrl.profPastdue.intBaseType) >= 0) {
					intBaseCustomAlgData.push(_intBaseCustomAlg);
				}
			}
			ctrl.intBaseCustomAlgs = intBaseCustomAlgData;
		}
		
		function resetIntRateIndexByIntBaseType(ctrl) {
			if (!ctrl.copyIntRateIndexOptions) return;
			var resultData = [];
			var rateIndexValueStr = "";
			if (ctrl.profPastdue.intBaseType == "F") {
				for (var i = 0; i < ctrl.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = ctrl.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var intIndexCode = intIndexObj.intIndexCode;
					var dataObj = {};
					dataObj.value = intIndexCode;
					//remove the sitecode, like 'MDBANK:'
					dataObj.label = intIndexCode.substring(intIndexObj.siteCode.length + intIndexObj.ccy.length + 2, intIndexCode.length);
					resultData.push(dataObj);
				}
			} else if (ctrl.profPastdue.intBaseType == "N" || ctrl.profPastdue.intBaseType == "C" || ctrl.profPastdue.intBaseType == "I") {
				for (var i = 0; i < ctrl.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = ctrl.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var tenorFlag = intIndexObj.tenorFlag;
					var dataObj = {};
					dataObj.value = intIndexObj.siteCode + ":" + intIndexObj.ccy + ":" + intIndexObj.intIndexName;
					dataObj.label = intIndexObj.intIndexName;
					if(tenorFlag == "Y" && rateIndexValueStr.indexOf(dataObj.value + ",") == -1) {
						rateIndexValueStr += dataObj.value + ",";
						resultData.push(dataObj);
					}
				}
			} else if (ctrl.profPastdue.intBaseType == "E") {
				resultData = [];
			} else {
				for (var i = 0; i < ctrl.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = ctrl.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var intIndexCode = intIndexObj.intIndexCode;
					var dataObj = {};
					dataObj.value = intIndexCode;
					//remove the sitecode, like 'MDBANK:'
					dataObj.label = intIndexCode.substring(intIndexObj.siteCode.length + intIndexObj.ccy.length + 2, intIndexCode.length);
					resultData.push(dataObj);
				}
			}
			ctrl.intRateIndexOptions = resultData;
		}
		
		// this must be out of ajax
		ctrl.initSubTableDefine = function() {
			//init pastdueSchds
			if(ctrl.validatorConfig && ctrl.validatorConfig.profPastdues) ctrl.layoutConfig.formConfigs.pastdueSchd.tableConfigs.table.validatorConfig = ctrl.validatorConfig.profPastdues.pastdueSchds;
			ctrl.pastdueSchdInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.pastdueSchd.tableConfigs.table, ctrlPastdueSchdRowSelectionChanged);
			ctrl.pastdueSchdInnerGridOptions.data = 'vm.profPastdue.pastdueSchds';
			function ctrlPastdueSchdRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedPastdueSchds = selectedRows;
			}
			//init tierCriterias
			if(ctrl.validatorConfig && ctrl.validatorConfig.profPastdues) ctrl.layoutConfig.formConfigs.custTier.tableConfigs.table.validatorConfig = ctrl.validatorConfig.profPastdues.tierBaseRateCusts;
			ctrl.custTierInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.custTier.tableConfigs.table, custTierRowSelectionChanged);
			ctrl.custTierInnerGridOptions.data = 'vm.profPastdue.tierBaseRateCusts';
			function custTierRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedCustTiers = selectedRows;
			}
			//init tierCriterias
			if(ctrl.validatorConfig && ctrl.validatorConfig.profPastdues) ctrl.layoutConfig.formConfigs.mixTier.tableConfigs.table.validatorConfig = ctrl.validatorConfig.profPastdues.tierBaseRateMixs;
			ctrl.mixTierInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.mixTier.tableConfigs.table, mixTierRowSelectionChanged);
			ctrl.mixTierInnerGridOptions.data = 'vm.profPastdue.tierBaseRateMixs';
			function mixTierRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedMixTiers = selectedRows;
			}
		};
		
		/*******************Pastdue Schedule**********************/
		function initPastdueSchd() {
			ctrl.addPastdueSchd = function(currRow) {
				var messages = [];
				var msg1 = validatePastdueSchd(ctrl);
				if (msg1) messages.push(msg1);
				if (messages && messages.length > 0) {
					modalService.openErrorDialogs({
						'messages' : messages
					});
					return;
				}
				if (!ctrl.profPastdue.pastdueSchds) {
					ctrl.profPastdue.pastdueSchds = [];
				}
				var pastdueTenorFrom;
				if (ctrl.profPastdue.pastdueSchds && ctrl.profPastdue.pastdueSchds.length > 0) {
					var prePastdueTenor = ctrl.profPastdue.pastdueSchds[ctrl.profPastdue.pastdueSchds.length - 1].pastdueTenor;
					pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
				} else {
					pastdueTenorFrom = 1;
				}
				currRow.pastdueTenorFrom = pastdueTenorFrom;
			};
			
			ctrl.removePastdueSchd = function() {
				angular.forEach(ctrl.selectedPastdueSchds, function(selectedPastdueSchd, key) {
					angular.forEach(ctrl.profPastdue.pastdueSchds, function(pastdueSchd, index) {
						if (pastdueSchd.$$hashKey == selectedPastdueSchd.$$hashKey) {
							// need exclude, will be remove by framework
							pastdueSchd.removeSelectedFlag = true;
						}
					});
				});
				//
				if (ctrl.profPastdue.pastdueSchds && ctrl.profPastdue.pastdueSchds.length > 0) {
					resetPastdueTenorFrom();
				}
			};
			
			ctrl.pastdueTenorChanged = function(rowObj, form, firstLoad) {
				resetPastdueTenorFrom();
			};
			
			function resetPastdueTenorFrom() {
				var allRowDatas = ctrl.profPastdue.pastdueSchds;
				var pastdueSchdList = [];
				angular.forEach(allRowDatas, function(pastdueSchd, key) {
					// need exclude, will be remove by framework
					if (!pastdueSchd.removeSelectedFlag) pastdueSchdList.push(pastdueSchd);
				});
				
				for (var i = 0; i < pastdueSchdList.length; i++) {
					var pastdue = pastdueSchdList[i];
					if (i == 0) {
						pastdue.pastdueTenorFrom = 1;
					}
					if(i<(pastdueSchdList.length-1) && pastdue.pastdueTenor){
						var nextPastdue = pastdueSchdList[i+1];
						nextPastdue.pastdueTenorFrom = arithService.add(pastdue.pastdueTenor, 1);
					}
				}
			}
		}
		
		function validatePastdueSchd(ctrl, isFinalCheck) {
			if (isFinalCheck && (!ctrl.profPastdue.pastdueSchds || ctrl.profPastdue.pastdueSchds.length == 0)) {
				var msg = 'Past Due Schedule is Required.';
				return msg;
			}
			if (ctrl.profPastdue.pastdueSchds) {
				var lastTener;
				// issue41953: Suggest To n-th Date in the bottom can be null
				for (var i = 0; i < ctrl.profPastdue.pastdueSchds.length; i++) {
					var pastdueSchd = ctrl.profPastdue.pastdueSchds[i];
					if (i == ctrl.profPastdue.pastdueSchds.length - 1) {
						// last row
						if (!pastdueSchd.pastdueTenor) pastdueSchd.pastdueTenor = 0;
					} else {
						// 0->last-1 rows
						var pastdueTenorFrom = pastdueSchd.pastdueTenorFrom;
						var pastdueTenor = pastdueSchd.pastdueTenor;
						var pastdueGrace = pastdueSchd.pastdueGrace;
						if (!pastdueTenor) {
							var msg = '"To n-th Day (included)" is Required in row ' + (i+1);
							return msg;
						}
//					if (!pastdueSchd.pastdueGrace) {
//						var msg = '"Grace Period Day(s)" is Required in row ' + (i+1);
//						return msg;
//					}
						if (!pastdueSchd.pastduePenalty) {
							var msg = '"Penalty Rate (%)" is Required in row ' + (i+1);
							return msg;
						}
						if (arithService.lte(pastdueTenor, pastdueTenorFrom)) {//tener<=tenerFrom
							var msg = '"To n-th Day (included)" should be bigger than "From n-th Day (included)" in row ' + (i+1);
							return msg;
						}
						// tenor = ((To n-th Day (included) - From n-th Day (included)) + 1)
						var tenor = arithService.add(arithService.sub(pastdueTenor, pastdueTenorFrom), 1);
						// pastdueGrace < tenor, otherwise popup error
						if (!arithService.lt(pastdueGrace, tenor)) {
							var msg = 'Grace Period Day(s) should be less than Tenor in row ' + (i+1);
							return msg;
						}
						if (i == 0) {
							lastTener = pastdueTenor;
							continue;
						}
						var currTenerFromNeedValue = arithService.add(lastTener, 1);
						if (!arithService.eq(pastdueTenorFrom, currTenerFromNeedValue)) {
							var msg = '"From n-th Day (included)" must be equal previous "To n-th Day (included)" + 1 in row ' + (i+1);
							return msg;
						}
						
						//
						lastTener = pastdueTenor;
					}
				}
			}
		}
		/*******************Pastdue Schedule**********************/
		
		/*******************Customer Tier**********************/
		function initCustTier() {
			ctrl.addCustTier = function(currRow) {
				var messages = [];
				var msg2 = validateCustTier(ctrl);
				if (msg2) messages.push(msg2);
				if (messages && messages.length > 0) {
					modalService.openErrorDialogs({
						'messages' : messages
					});
					return;
				}
				if (!ctrl.profPastdue.tierBaseRateCusts) {								
					ctrl.profPastdue.tierBaseRateCusts = [];
				}
				var tenerFrom;
				if (ctrl.profPastdue.tierBaseRateCusts && ctrl.profPastdue.tierBaseRateCusts.length > 0) {
					var preCustTierTenor = ctrl.profPastdue.tierBaseRateCusts[ctrl.profPastdue.tierBaseRateCusts.length - 1].tener;
//					tenerFrom = arithService.add(preCustTierTenor, 1);
					tenerFrom = preCustTierTenor;
				} else {
					tenerFrom = 0;
				}
				currRow.tenerFrom = tenerFrom;
			};
			
			ctrl.removeCustTier = function() {		
				angular.forEach(ctrl.selectedCustTiers, function(selectedCustTier, key) {
					angular.forEach(ctrl.profPastdue.tierBaseRateCusts, function(custTier, index) {
						if (custTier.$$hashKey == selectedCustTier.$$hashKey) {
							// need exclude, will be remove by framework
							custTier.removeSelectedFlag = true;
						}
					});
				});
				//
				caculateTenorFrom();
			};
			
			ctrl.custTierMoreDataDyna = function (obj){
				var params = {};
				// for get dynaparm page
				params.parmSubcat = obj.alg;
				// for data on edit page popup
				params.custDynaparm = {};
				params.custDynaparm.dynamicParams = obj.dynamicParams;
				modalService.openCustomDialogs({
				   'title': 'Cust More Data',
				   'dialogsModule' : 'cda.custdynaparm.popupedit',
				   'params': params,
				   'btnCallback' : {
						'ok' : function(resultList) {
							obj.dynamicParams = resultList.dynamicParams;
						}
					}	
				});
			};
			
			ctrl.tenorChanged = function () {
				caculateTenorFrom();
			}
			
			function caculateTenorFrom() {
				var allRowDatas = ctrl.profPastdue.tierBaseRateCusts;
				var rowDatas = [];
				angular.forEach(allRowDatas, function(tierBaseRateCust, key) {
					// need exclude, will be remove by framework
					if (!tierBaseRateCust.removeSelectedFlag) rowDatas.push(tierBaseRateCust);
				});
				
				if(rowDatas && rowDatas.length > 0) {
					for(var i = 0; i < rowDatas.length; i++) {
						var preIndex = i - 1;
						if(preIndex >= 0) {
							rowDatas[i].tenerFrom = rowDatas[preIndex].tener;
						} else {
							rowDatas[i].tenerFrom = 0;
						}
					}
				}
			}
		}
		
		function validateCustTier(ctrl) {
			if (ctrl.profPastdue.tierBaseRateCusts && ctrl.profPastdue.intBaseType == 'C') {
				var lastTener;
				for (var i = 0; i < ctrl.profPastdue.tierBaseRateCusts.length; i++) {
					var tierBaseRateCust = ctrl.profPastdue.tierBaseRateCusts[i];
					var tenerFrom = tierBaseRateCust.tenerFrom;
					var tener = tierBaseRateCust.tener;
					if (!tener) {
						var msg = 'To(Exclude) is Required!';
						return msg;
					}
					if (!tierBaseRateCust.alg) {
						var msg = 'Cal Scheme is Required!';
						return msg;
					}
					if (arithService.lte(tener, tenerFrom)) {//tener<=tenerFrom
						var msg = 'To(Exclude) must bigger then From in row ' + (i+1);
						return msg;
					}
					if (i == 0) {
						lastTener = tener;
						continue;
					}
//					var currTenerFromNeedValue = arithService.add(lastTener, 1);
					var currTenerFromNeedValue = lastTener;
					if (!arithService.eq(tenerFrom, currTenerFromNeedValue)) {
						var msg = 'From must be equal to previous To(Exclude) in row ' + (i+1);
						return msg;
					}
					//
					lastTener = tener;
				}
			}
		}
		/*******************Customer Tier**********************/
		
		/*******************Mix Tier**********************/
		function initMixTier() {
		}
		
		function validateMixTier(ctrl) {
			if (ctrl.profPastdue.tierBaseRateMixs && ctrl.profPastdue.intBaseType == 'M') {
				for (var i = 0; i < ctrl.profPastdue.tierBaseRateMixs.length; i++) {
					var tierBaseRateMix = ctrl.profPastdue.tierBaseRateMixs[i];
					if (!tierBaseRateMix.index) {
						var msg = 'Index is Required!';
						return msg;
					}
				}
			}
		}
		/*******************Mix Tier**********************/
		
		ctrl.ok = function() {
			if ($scope.profPastdueForm) {
				$scope.profPastdueForm.$setSubmitted();
				if(!$scope.profPastdueForm.$valid) {
					dialogsModule.showErrorMessages($scope.profPastdueForm);
					return;
				}
			}
			// if such as length no pass, model will be undefined always 
			var messages = [];
			var msg1 = validatePastdueSchd(ctrl, true);
			if (msg1) messages.push(msg1);
			var msg2 = validateCustTier(ctrl);
			if (msg2) messages.push(msg2);
			var msg3 = validateMixTier(ctrl);
			if (msg3) messages.push(msg3);
			if (messages && messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
				return;
			}
			if ((!$scope.profPastdueForm || $scope.profPastdueForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.profPastdue) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
